package com.ruoyi.wechat.api;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wechat.domain.Appointment;
import com.ruoyi.wechat.domain.Order;
import com.ruoyi.wechat.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Api(value = "微信小程序API-营地订单", tags = {"微信小程序API-营地订单"})
@RestController
@RequestMapping("/wechat/order")
public class WxOrderController  extends BaseController {

    @Autowired
    private IOrderService orderService;


    /**
     * 新增营地订单
     */
    @ApiOperation("新增营地订单")
    @Log(title = "营地订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Order order)
    {
        System.out.println("order: "+order);
        order.setOrderTime(new Date());
//        order.setStatus(2L);
        String key = orderService.WxInsertOrder(order);
        System.out.println("order uuid:"+key);
        return success(key);
    }

    /**
     * 查询营地订单列表
     */
    @ApiOperation("查询营地订单列表")
    @GetMapping("/list")
    public TableDataInfo list(Order order)
    {
//        startPage();
        List<Order> list = orderService.selectOrderList(order);
        return getDataTable(list);
    }

    /**
     * 修改营地订单
     */
    @ApiOperation("修改营地订单")
    @Log(title = "营地订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Order order)
    {

        Order order1 = orderService.selectOrderByOrderId(order.getOrderId());
        if (order1==null){
            return AjaxResult.error("该订单不存在");
        }
        if (order1.getStatus()==3){
            return AjaxResult.error("该订单已核销");
        }
        return toAjax(orderService.updateOrder(order));
    }
}
